package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 沈明月
 * Date: 2023-06-30
 * Time: 9:38
 */
public class DelOperation implements IOperation {
    public void work(BookList bookList){
        System.out.println("删除图书！");

        int currentSize = bookList.getUsedSize();
        if(currentSize == 0){
            System.out.println("书架为空，不能删除。");
            return;
        }

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除的书的书名：");
        String name = scanner.nextLine();

        //找到要删除的书

        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book1 = bookList.getBook(i);
            if(book1.getName().equals(name)){
                index = i;
                break;
            }
        }
        //2.index ！= -1有这本书，删
        if(index == -1){
            System.out.println("没有你要删的书！");
            return;
        }
        for (int i = 0; i < currentSize-1; i++) {
            Book book1 = bookList.getBook(i+1);
            bookList.setBooks(i,book1);
        }
        //书删掉以后，要维持usedSize
        bookList.setUsedSize(currentSize-1);




    }
}
